package com.vict.controller.app.appbase;

import com.alibaba.fastjson.JSONObject;
import com.vict.bean.app.appbase.ao.CheckUpdateAO;
import com.vict.bean.app.appbase.vo.CheckUpdateVO;
import com.vict.bean.appconfig.dto.AppConfigDTO;
import com.vict.bean.appconfig.vo.AppConfigVO;
import com.vict.bean.appversion.vo.AppVersionVO;
import com.vict.bean.appversion.vo.SelectNowVersionVO;
import com.vict.entity.AppVersion;
import com.vict.enums.app.Platform;
import com.vict.framework.bean.R;
import com.vict.framework.keyvalue.entity.KeyValue;
import com.vict.framework.web.ApiPrePath;
import com.vict.mapper.AppVersionMapper;
import com.vict.utils.KeyValueUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Optional;

@ApiPrePath
@Api
@Slf4j
@RestController
@RequestMapping("/app/appBase")
public class AppBaseController {
    private static final String SEAGGER_TAGS = "app基础配置";

    @Autowired
    private AppVersionMapper appVersionMapper;

    @ApiOperation(tags = SEAGGER_TAGS, value = "获取app配置", httpMethod = "POST")
    @PostMapping("/getAppConfig")
    public R<AppConfigVO> getAppConfig() {
        KeyValue appConfig = KeyValueUtil.getValueByKey("appConfig");

        AppConfigDTO appConfigDTO = Optional.ofNullable(appConfig).map(o -> o.getValue1())
                .map(o -> o.trim())
                .filter(o -> !o.equals(""))
                .filter(o -> o.startsWith("{"))
                .map(o -> JSONObject.parseObject(o, AppConfigDTO.class))
                .orElse(null);

        AppConfigVO appConfigVO = new AppConfigVO();

        KeyValue appDebugModeKeyValue = KeyValueUtil.getValueByKey("app_debugMode");
        appConfigVO.setDebugMode(appDebugModeKeyValue.getValue1());

        KeyValue appLogSwitchKeyValue = KeyValueUtil.getValueByKey("app_logSwitch");
        appConfigVO.setLogSwitch(appLogSwitchKeyValue.getValue1());

        appConfigVO.setTabBarList(appConfigDTO.getTabBarList());

        appConfigVO.setDisableTabBarList(appConfigDTO.getDisableTabBarList());

        AppVersion android = appVersionMapper.selectVersion(Platform.Android);
        AppVersion ios = appVersionMapper.selectVersion(Platform.IOS);


        AppVersionVO androidAppVersionVO = new AppVersionVO();
        androidAppVersionVO.setAppVersion(android);

        AppVersionVO iosAppVersionVO = new AppVersionVO();
        iosAppVersionVO.setAppVersion(ios);

        SelectNowVersionVO selectNowVersionVO = new SelectNowVersionVO();
        selectNowVersionVO.setAndroid(androidAppVersionVO);
        selectNowVersionVO.setIos(iosAppVersionVO);

        appConfigVO.setAppNowVersion(selectNowVersionVO);

        return R.ok(appConfigVO);
    }

    @ApiOperation(tags = SEAGGER_TAGS, value = "获取升级方式配置", httpMethod = "POST")
    @PostMapping("/checkUpdate")
    public R<CheckUpdateVO> checkUpdate(@RequestBody CheckUpdateAO checkUpdateAO){
        checkUpdateAO.check();

        if(checkUpdateAO.getPlatform() == Platform.Android){

            Integer nowVersion = AppVersion.convertVersionNum(checkUpdateAO.getNowVersion());
            Integer updateVersion = AppVersion.convertVersionNum(checkUpdateAO.getUpdateVersion());

            AppVersion oneMethodApk = appVersionMapper.selectOneMethod(Platform.Android, AppVersion.UpdateMethod.APK, nowVersion, updateVersion);
            AppVersion oneForce = appVersionMapper.selectOneForce(Platform.Android, nowVersion, updateVersion);

            CheckUpdateVO checkUpdateVO = new CheckUpdateVO();
            checkUpdateVO.setOneMethodApk(oneMethodApk);
            checkUpdateVO.setOneForce(oneForce);

            return R.ok(checkUpdateVO);
        }else if(checkUpdateAO.getPlatform() == Platform.IOS){
            Integer nowVersion = AppVersion.convertVersionNum(checkUpdateAO.getNowVersion());
            Integer updateVersion = AppVersion.convertVersionNum(checkUpdateAO.getUpdateVersion());

            AppVersion oneMethodApk = appVersionMapper.selectOneMethod(Platform.IOS, AppVersion.UpdateMethod.IPA, nowVersion, updateVersion);
            AppVersion oneForce = appVersionMapper.selectOneForce(Platform.IOS, nowVersion, updateVersion);

            CheckUpdateVO checkUpdateVO = new CheckUpdateVO();
            checkUpdateVO.setOneMethodApk(oneMethodApk);
            checkUpdateVO.setOneForce(oneForce);

            return R.ok(checkUpdateVO);
        }
        return R.ok();
    }
}
